package concurrecy;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class ThreadLocalHolder {
    private static ThreadLocal<Integer> threadLocal = new ThreadLocal<>();
    public static int get() {
        return threadLocal.get();
    }

    public static void init() {
        threadLocal.set(0);
    }

    public static void increment() {
        threadLocal.set(threadLocal.get() + 1);
    }
}

public class ThreadLocalDemo {
    public static void main(String[] args) {
        ExecutorService service = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            service.execute(() -> {
                ThreadLocalHolder.init();
                for (int j = 0; j < 500000; j++) {
                    ThreadLocalHolder.increment();
                }
                System.out.println(Thread.currentThread() + " : " + ThreadLocalHolder.get());
            });
        }
        service.shutdown();
    }
}
